library(interplot); library(stargazer); library(plyr); library(xtable)
library(lme4); library(arm); library(data.table); library(lmtest)

setwd("INSERT PATH")
pd<-read.csv('party_day_JOP_Pereira.csv', stringsAsFactors = F)

#%#%#%#%#%#%
#TABLE 1

set.seed(153)
mlm2_simp<-glmer(cons~cons_lag+
                   vdiff*niche+
                   (1|camp), family=binomial, pd)

set.seed(1512)
mlm2<-glmer(cons~cons_lag+
              vdiff*niche+
              daystoelec+
              poldist+
              enp+
              propdayscovered+
              cohthey_0day+
              pm+
              (1|camp), family=binomial, pd)

set.seed(521)
mlm3<-glmer(cons~cons_lag+
              vdiff*niche+
              daystoelec+
              poldist+
              enp+
              propdayscovered+
              cohthey_0day+
              pm+
              (1|party_camp), family=binomial, pd)
ss <- getME(mlm3,c("theta","fixef"))
mlm3 <- update(mlm3,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))


stargazer(mlm2_simp,mlm2,mlm3, digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space=T)


#%#%#%#%#%#%
# # PREDICTIONS (described in text)
preds_main1<-predict(mlm2,data.frame(cons_lag=mean(na.omit(pd$cons_lag)),
                                     vdiff=0, 
                                     niche=0,
                                     daystoelec=15,
                                     propdayscovered=mean(pd$propdayscovered),
                                     enp=mean(pd$enp),
                                     camp='UK2010',
                                     poldist=mean(pd$poldist),
                                     pm=mean(pd$pm),
                                     cohthey_0day=mean(pd$cohthey_0day),
                                     party_camp='LabourUK2010'
), type='response', allow.new.level=T)

preds_main2<-predict(mlm2,data.frame(cons_lag=mean(na.omit(pd$cons_lag)),
                                     vdiff=5, 
                                     niche=0,
                                     daystoelec=15,
                                     camp='UK2010',
                                     enp=mean(pd$enp),
                                     poldist=mean(pd$poldist),
                                     cohthey_0day=mean(pd$cohthey_0day),
                                     pm=mean(pd$pm),
                                     propdayscovered=mean(pd$propdayscovered),
                                     party_camp='LabourUK2010'
), type='response', allow.new.level=T)

#Difference in probability of an ideologically consistent set of statements for mainstream parties
  #In response to 5-point decrease in campaign performance
difprob<-preds_main1-preds_main2
difprob/mean(na.omit(pd$cons[pd$any_ideost>0&pd$niche==0]))


preds_niche1<-predict(mlm2,data.frame(cons_lag=mean(na.omit(pd$cons_lag)),
                                      vdiff=0, 
                                      niche=1,
                                      daystoelec=15,
                                      propdayscovered=mean(pd$propdayscovered),
                                      enp=mean(pd$enp),
                                      camp='UK2010',
                                      poldist=mean(pd$poldist),
                                      pm=mean(pd$pm),
                                      cohthey_0day=mean(pd$cohthey_0day),
                                      party_camp='LabourUK2010'
), type='response', allow.new.level=T)

preds_niche2<-predict(mlm2,data.frame(cons_lag=mean(na.omit(pd$cons_lag)),
                                      vdiff=5, 
                                      niche=1,
                                      daystoelec=15,
                                      camp='UK2010',
                                      enp=mean(pd$enp),
                                      poldist=mean(pd$poldist),
                                      cohthey_0day=mean(pd$cohthey_0day),
                                      pm=mean(pd$pm),
                                      propdayscovered=mean(pd$propdayscovered),
                                      party_camp='LabourUK2010'
), type='response', allow.new.level=T)

#Difference in probability of an ideologically consistent set of statements for niche parties
#In response to 5-point decrease in campaign performance
difprob<-preds_niche1-preds_niche2
difprob/mean(na.omit(pd$cons[pd$any_ideost>0&pd$niche==1]))

#Difference between niche and mainstream when campaign perfomance = 0
preds_niche1-preds_main1
#Difference between niche and mainstream when campaign perfomance = 5
preds_niche2-preds_main2


#%#%#%#%#%#%
#FIGURE 1

interplot(mlm2,'niche','vdiff', rfill='aquamarine3', xmin=-15, xmax=15, hist=T)+
  geom_rug(data = subset(pd, niche == 1),
           aes(x = vdiff), inherit.aes = F)+
  xlim(-15,15)+
  theme_bw()+ 
  theme(panel.grid.minor = element_blank(),axis.text=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        axis.title=element_text(size=14))+
  ylab("Marginal effect of Niche")+
  ggtitle('Marginal effect of Niche, by campaign performance')+
  xlab("Campaign Performance (percentage points)")+
  geom_hline(yintercept = 0, linetype = "dashed")+geom_line(color='aquamarine4')


#%#%#%#%#%#%
#FIGURE 2

#Get Estimate and SE of vdiff for niche
est_main<-fixef(mlm2)[3]
se_main<-sqrt(vcov(mlm2)[3,3])
main_cis95<-est_main+c(-1,1)*1.96*se_main
main_cis90<-est_main+c(-1,1)*1.65*se_main
est_niche<-fixef(mlm2)[3]+fixef(mlm2)[11]
se_niche<-sqrt(vcov(mlm2)[3,3] + vcov(mlm2)[11,11] + 2*vcov(mlm2)[3,11])
niche_cis95<-est_niche+c(-1,1)*1.96*se_niche
niche_cis90<-est_niche+c(-1,1)*1.65*se_niche

par(mar=c(2,4,3,1))
coefplot(c(est_niche,est_main),c(se_niche,se_main), 
         col.pts=c("aquamarine3"), cex.pts=1.4, cex.var=1, mar=c(1,5,4,2),oma=c(2,1,0,1),
         varnames=rev(c("Mainstream"
                        , "Niche")), main="", lower.conf.bounds=c(niche_cis95[1],main_cis95[1]),
         upper.conf.bounds=c(niche_cis95[2],main_cis95[2]),
         
         xlim=c(-.15,.15))
lines(c(main_cis90[1],main_cis90[2]),c(2,2),col='aquamarine3',lwd=3)
lines(c(niche_cis90[1],niche_cis90[2]),c(1,1),col='aquamarine3',lwd=3)
mtext("Marginal effects of campaign performance, by party type", outer=F, line=2.5, cex=1)



#%#%#%#%#%#%
#TABLE 2
set.seed(5231)
manifs1<-glmer(dom_avissue_st~
                 vdiff+
                 daystoelec+
                 poldist+
                 cohthey_0day+
                 propdayscovered+
                 dom_avissue_share+
                 niche+
                 pm+
                 enp+(1|camp),family=binomial, pd)

set.seed(521)
manifs2<-glmer(dom_avissue_st~
                 vdiff*niche+
                 daystoelec+
                 poldist+
                 cohthey_0day+
                 propdayscovered+
                 dom_avissue_share+
                 pm+ 
                 enp+(1|camp),family=binomial, pd)
ss <- getME(manifs2,c("theta","fixef"))
manifs2 <- update(manifs2,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(manifs1,manifs2, digits=2,no.space = T,
          covariate.labels=c('Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'Others\' Statements', '\\% Days Covered', 
                             '\\% Dominant Issue', 'Incumbent', 'ENP', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'))


#%#%#%#%#%#%
# # PREDICTIONS (described in text)
preds_manifs1<-predict(manifs1,data.frame(dom_avissue_share=mean(na.omit(pd$dom_avissue_share)),
                                          vdiff=0, 
                                          niche=mean(pd$niche),
                                          daystoelec=15,
                                          propdayscovered=mean(pd$propdayscovered),
                                          enp=mean(pd$enp),
                                          camp='UK2010',
                                          pm=mean(pd$pm),
                                          poldist=mean(pd$poldist),
                                          cohthey_0day=mean(pd$cohthey_0day),
                                          party_camp='LabourUK2010'
), type='response', allow.new.level=T)

preds_manifs2<-predict(manifs1,data.frame(dom_avissue_share=mean(na.omit(pd$dom_avissue_share)),
                                          vdiff=5, 
                                          niche=mean(pd$niche),
                                          daystoelec=15,
                                          propdayscovered=mean(pd$propdayscovered),
                                          enp=mean(pd$enp),
                                          pm=mean(pd$pm),
                                          camp='UK2010',
                                          poldist=mean(pd$poldist),
                                          cohthey_0day=mean(pd$cohthey_0day),
                                          party_camp='LabourUK2010'
), type='response', allow.new.level=T)

#Difference in probability of raising dominant issues for 5-point increase in campaign performance.
(preds_manifs2-preds_manifs1)/mean(na.omit(pd$dom_avissue_st))

#Comparing model fit
lrtest(manifs1,manifs2)


#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#%#%#%#%#%#%#%#%#%#%#%#% APPENDIX
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX A
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

##To get number of article and policy statements
#loading CCDP dataset cleaned for typos
self<-read.csv('self_clean.csv', stringsAsFactors = F)
self$self_sts<-ifelse(self$statement_type%in% c('SelfIssue', 'SelfIssueVal'),1,0)

#Column 4 from Table A1
sts_by_camp<-aggregate(self$self_sts, list(self$country_elec), sum)
col4<-sts_by_camp[,2]

#Column 3 from Table A1
col3<-ddply(self,~country_elec,summarise,count=length(unique(article_number)))[,2]

##To get share polls per day
npts<-aggregate(pd$party,list(pd$camp), function(x) length(unique(x)))
#Gives number of days with new polls
days_w_new_polls<-table(pd$camp,pd$poll)/npts$x
#Gives number of campaign days in dataset
camp_days<-aggregate(pd$camp.day,list(pd$camp),function(x) length(unique(x)))

#Column 5 from Table A1
col5<-round(days_w_new_polls[,2]/camp_days$x,2)

country<-c('Czech Republic','' , 'Denmark', '', 'Germany','' , 'Hungary', '',
           'Poland','' ,'Portugal','' , 'Spain', '', 'Sweden','' , 'UK', '','' , 'The Netherlands')
yr<-substr(sts_by_camp[,1],3,6)

ord<-c(1,2,5,6,3,4,9,10,12,13,14,15,7,8,16,17,18,19,20,11)
tableA1<-as.data.frame(cbind(country,yr,col3[ord],col4[ord],col5[ord]))
tableA1

#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX B - Alternative operationalizations of campaign rhetoric  
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#Uploading campaign rhetoric based on different coding decisions
altr<-read.csv('alternative_rhetoric_codings.csv', stringsAsFactors = F)
pd<-cbind(pd,altr[,-1])

set.seed(4221)
mlm2_alt1<-glmer(cons_alt1~cons_alt1_lag+
                 vdiff*niche+
                 daystoelec+
                 poldist+
                 enp+
                 propdayscovered+
                 cohthey_0day+
                 pm+
                 (1|camp), family=binomial, pd)
ss <- getME(mlm2_alt1,c("theta","fixef"))
mlm2_alt1 <- update(mlm2_alt1,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))
mlm2_alt2<-glmer(cons_alt2~cons_alt2_lag+
                   vdiff*niche+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|camp), family=binomial, pd)
ss <- getME(mlm2_alt2,c("theta","fixef"))
mlm2_alt2 <- update(mlm2_alt2,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))
mlm2_alt3<-glmer(cons_alt3~cons_alt3_lag+
                   vdiff*niche+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|camp), family=binomial, pd)
ss <- getME(mlm2_alt3,c("theta","fixef"))
mlm2_alt3 <- update(mlm2_alt3,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mlm2_alt1,mlm2_alt2,mlm2_alt3, digits=2,
          covariate.labels=c('Non-Centrist Rhetoric v1','Non-Centrist Rhetoric v2','Non-Centrist Rhetoric v3', 
                             'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space=T)

#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX c
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#Note: No analyses

#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX D
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

#%#%#%#%#%#%
# # Table D1 - Correlations btw measures with diff baselines
round(cor(pd[,c('vdiff','vdiff_1db','vdiff_1wb','vdiff_1mb','vdiff_2mb','vdiff_1yb','vdiff_2yb')],
          use='complete.obs')[1,],2)[-1]

#%#%#%#%#%#%
# # Table D2
set.seed(16321)
mlm2_1db<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_1db*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_1db,c("theta","fixef"))
mlm2_1db <- update(mlm2_1db,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

mlm2_1wb<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_1wb*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_1wb,c("theta","fixef"))
mlm2_1wb <- update(mlm2_1wb,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

mlm2_1mb<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_1mb*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_1mb,c("theta","fixef"))
mlm2_1mb <- update(mlm2_1mb,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

mlm2_2mb<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_2mb*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_2mb,c("theta","fixef"))
mlm2_2mb <- update(mlm2_2mb,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

mlm2_1yb<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_1yb*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_1yb,c("theta","fixef"))
mlm2_1yb <- update(mlm2_1yb,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

mlm2_2yb<-glmer(cons~cons_lag+
                  vdiff*niche+
                  vdiff_2yb*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), family=binomial, pd)
ss <- getME(mlm2_2yb,c("theta","fixef"))
mlm2_2yb <- update(mlm2_2yb,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mlm2_1db, mlm2_1wb, mlm2_1mb, mlm2_2mb, mlm2_1yb, mlm2_2yb,digits=2,
          star.char = c('+', '*', '**'), no.space = T)


#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX E
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

#%#%#%#%#%#%
# # Table E1 (see Table 1 above)

#%#%#%#%#%#%
# # Table E2 (see Table 2 above)

#%#%#%#%#%#%
# # Table E3 - Alternative measures of incumbency
set.seed(321)
mlm2_INC2<-glmer(cons~cons_lag+
                   vdiff*niche+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   junior+
                   (1|camp), family=binomial, pd)
#For the model to converge, run it a bit longer
ss <- getME(mlm2_INC2,c("theta","fixef"))
mlm2_INC2 <- update(mlm2_INC2,start=ss,control=glmerControl(optCtrl=list(maxfun=2e7)))
summary(mlm2_INC2)

set.seed(754)
mlm3_INC2<-glmer(cons~cons_lag+
                   vdiff*niche+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   junior+
                   (1|party_camp), family=binomial, pd)
#For the model to converge, run it a bit longer
ss <- getME(mlm3_INC2,c("theta","fixef"))
mlm3_INC2 <- update(mlm3_INC2,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mlm2_INC2,mlm3_INC2, digits=2,no.space = T,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Senior Coalition Partner', 'Junior Coalition Partner', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'))



#%#%#%#%#%#%
# # Table E4 - Fixed-effects models
fe1<-glm(cons~cons_lag+
           vdiff*niche+
           daystoelec+
           poldist+
           propdayscovered+
           cohthey_0day+
           pm+
           as.factor(camp),family=binomial(link='logit'),pd)
fe2<-glm(cons~cons_lag+
           vdiff*niche+
           daystoelec+
           poldist+
           propdayscovered+
           cohthey_0day+
           pm+
           as.factor(party),family=binomial(link='logit'),pd)

stargazer(fe1,fe2, digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent'),
          star.char = c('+', '*', '**'), no.space=T)


#%#%#%#%#%#%
# # Table E5 - Meguid's typology
set.seed(1632)
mod_soc<-glmer(cons~cons_lag+
                 vdiff*niche_alt+
                 daystoelec+
                 poldist+
                 enp+
                 propdayscovered+
                 cohthey_0day+
                 pm+
                 (1|camp), family=binomial, pd)
ss <- getME(mod_soc,c("theta","fixef"))
mod_soc <- update(mod_soc,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

set.seed(1633)
mod_soc3<-glmer(cons~cons_lag+
                  vdiff*niche_alt+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|party_camp), family=binomial, pd)
ss <- getME(mod_soc3,c("theta","fixef"))
mod_soc3 <- update(mod_soc3,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mod_soc,mod_soc3,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space = T)


#%#%#%#%#%#%
# # Table E6 - Challenger typology
set.seed(1632)
mlm2_chal<-glmer(cons~cons_lag+
                   vdiff*chal+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|camp), family=binomial, pd)

mlm3_chal<-glmer(cons~cons_lag+
                   vdiff*chal+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|party_camp), family=binomial, pd)
ss <- getME(mlm3_chal,c("theta","fixef"))
mlm3_chal <- update(mlm3_chal,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mlm2_chal,mlm3_chal,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Challenger', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Challenger'),
          star.char = c('+', '*', '**'), no.space = T)



#%#%#%#%#%#%
# # Table E7 - Continuous measure of campaign rhetoric

mlm2_cont<-lmer(coh_1day~coh_1day_lag+
                  vdiff*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|camp), pd)

mlm3_cont<-lmer(coh_1day~coh_1day_lag+
                  vdiff*niche+
                  daystoelec+
                  poldist+
                  enp+
                  propdayscovered+
                  cohthey_0day+
                  pm+
                  (1|party_camp), pd)

stargazer(mlm2_cont,mlm3_cont,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space = T)


#%#%#%#%#%#%
# # Table E8 - 3-days measure of campaign rhetoric
set.seed(311)
mlm23d<-glmer(cons3d~cons3d_lag+
                vdiff*niche+
                daystoelec+
                poldist+
                enp+
                propdayscovered+
                cohthey_0day+
                pm+
                (1|camp), family=binomial, pd)
ss <- getME(mlm23d,c("theta","fixef"))
mlm23d <- update(mlm23d,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

set.seed(236)
mlm33d<-glmer(cons3d~cons3d_lag+
                vdiff*niche+
                daystoelec+
                poldist+
                enp+
                propdayscovered+
                cohthey_0day+
                pm+
                (1|party_camp), family=binomial, pd)
ss <- getME(mlm33d,c("theta","fixef"))
mlm33d <- update(mlm33d,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

stargazer(mlm23d,mlm33d,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space = T)

# est_main<-fixef(mlm23d)[3]
# se_main<-sqrt(vcov(mlm23d)[3,3])
# main_cis95<-est_main+c(-1,1)*1.96*se_main
# main_cis90<-est_main+c(-1,1)*1.65*se_main
# est_niche<-fixef(mlm23d)[3]+fixef(mlm23d)[11]
# se_niche<-sqrt(vcov(mlm23d)[3,3] + vcov(mlm23d)[11,11] + 2*vcov(mlm23d)[3,11])
# niche_cis95<-est_niche+c(-1,1)*1.96*se_niche
# niche_cis90<-est_niche+c(-1,1)*1.65*se_niche
# 
# coefplot(c(est_niche,est_main),c(se_niche,se_main), 
#          col.pts=c("aquamarine3"), cex.pts=1.4, cex.var=1.2, mar=c(1,8,4,2),oma=c(2,1,0,1),
#          varnames=rev(c("Mainstream"
#                         , "Niche")), main="", lower.conf.bounds=c(niche_cis95[1],main_cis95[1]),
#          upper.conf.bounds=c(niche_cis95[2],main_cis95[2]),
#          
#          xlim=c(-.15,.15))
# lines(c(main_cis90[1],main_cis90[2]),c(2,2),col='aquamarine3',lwd=3)
# lines(c(niche_cis90[1],niche_cis90[2]),c(1,1),col='aquamarine3',lwd=3)
# mtext("Marginal effects of campaign performance, by party type", outer=F, line=2.5, cex=1)


#%#%#%#%#%#%
# # Table E9 - Western and Eastern-Central Europe
set.seed(152)
mlm2_west<-glmer(cons~cons_lag+
              vdiff*niche+
              daystoelec+
              poldist+
              enp+
              propdayscovered+
              cohthey_0day+
              pm+
              (1|camp), family=binomial, pd[pd$country %in% c('DK', 'DE', 'NL', 'PO', 'SV', 'ES', 'UK'),])
ss <- getME(mlm2_west,c("theta","fixef"))
mlm2_west <- update(mlm2_west,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

set.seed(1521)
mlm2_CEE<-glmer(cons~cons_lag+
                   vdiff*niche+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|camp), family=binomial, pd[pd$country %in% c('DK', 'DE', 'NL', 'PO', 'SV', 'ES', 'UK') !=T,])

stargazer(mlm2_west,mlm2_CEE,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space = T)

#%#%#%#%#%#%
# # Table E10 - High/Low coverage campaigns
bel50<-pd$camp%in%c('CZ2010', 'CZ2013', 'HU2006', 'HU2010', 'PL2007')

set.seed(15212)
mlm2_over50<-glmer(cons~cons_lag+
                     vdiff*niche+
                     daystoelec+
                     poldist+
                     enp+
                     propdayscovered+
                     cohthey_0day+
                     pm+
                     (1|camp), family=binomial, pd[bel50!=T,])
ss <- getME(mlm2_over50,c("theta","fixef"))
mlm2_over50 <- update(mlm2_over50,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

set.seed(152)
mlm2_bel50<-glmer(cons~cons_lag+
                    vdiff*niche+
                    daystoelec+
                    poldist+
                    enp+
                    propdayscovered+
                    cohthey_0day+
                    pm+
                    (1|camp), family=binomial, pd[bel50,])

stargazer(mlm2_over50,mlm2_bel50,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Niche', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent', 'Campaign Performance times Niche'),
          star.char = c('+', '*', '**'), no.space = T)



#%#%#%#%#%#%
# # Table E11 - By party type
mlm2_main<-glmer(cons~cons_lag+
                   vdiff+
                   daystoelec+
                   poldist+
                   enp+
                   propdayscovered+
                   cohthey_0day+
                   pm+
                   (1|camp), family=binomial, pd[pd$niche==0,])
mlm2_niche<-glmer(cons~cons_lag+
                    vdiff+
                    daystoelec+
                    poldist+
                    enp+
                    propdayscovered+
                    cohthey_0day+
                    #pm+
                    (1|camp), family=binomial, pd[pd$niche==1,])

stargazer(mlm2_main,mlm2_niche,digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Campaign Performance', 'Days to Election',
                             'Poll Distance', 'ENP', '\\% Days Covered', 'Others\' Statements', 
                             'Incumbent'),
          star.char = c('+', '*', '**'), no.space = T)

#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX F - Incumbency status and campaign responsiveness
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

#Interacting performance with incumbency
mlm2_inc1_int<-glmer(cons~cons_lag+
                      vdiff*incumbent+
                      daystoelec+
                      poldist+
                      enp+
                      propdayscovered+
                      cohthey_0day+
                      (1|party_camp), family=binomial, pd)
ss <- getME(mlm2_inc1_int,c("theta","fixef"))
mlm2_inc1_int <- update(mlm2_inc1_int,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

#Get Estimate and SE of vdiff for niche
est_inc1<-fixef(mlm2_inc1_int)[3]
se_inc1<-sqrt(vcov(mlm2_inc1_int)[3,3])
inc1_cis95<-est_inc1+c(-1,1)*1.96*se_inc1
inc1_cis90<-est_inc1+c(-1,1)*1.65*se_inc1
est_opp1<-fixef(mlm2_inc1_int)[3]+fixef(mlm2_inc1_int)[10]
se_opp1<-sqrt(vcov(mlm2_inc1_int)[3,3] + vcov(mlm2_inc1_int)[10,10] + 2*vcov(mlm2_inc1_int)[3,10])
opp1_cis95<-est_opp1+c(-1,1)*1.96*se_opp1
opp1_cis90<-est_opp1+c(-1,1)*1.65*se_opp1

#Interacting performance with senior coalition partner
mlm2_pm_int<-glmer(cons~cons_lag+
                     vdiff*pm+
                     daystoelec+
                     poldist+
                     enp+
                     propdayscovered+
                     cohthey_0day+
                     (1|party_camp), family=binomial, pd[pd$junior==0,])
#For the model to converge I have torun it a bit longer
ss <- getME(mlm2_pm_int,c("theta","fixef"))
mlm2_pm_int <- update(mlm2_pm_int,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))


#Get Estimate and SE of vdiff for niche
est_inc2<-fixef(mlm2_pm_int)[3]
se_inc2<-sqrt(vcov(mlm2_pm_int)[3,3])
inc2_cis95<-est_inc2+c(-1,1)*1.96*se_inc2
inc2_cis90<-est_inc2+c(-1,1)*1.65*se_inc2
est_opp2<-fixef(mlm2_pm_int)[3]+fixef(mlm2_pm_int)[10]
se_opp2<-sqrt(vcov(mlm2_pm_int)[3,3] + vcov(mlm2_pm_int)[10,10] + 2*vcov(mlm2_pm_int)[3,10])
opp2_cis95<-est_opp2+c(-1,1)*1.96*se_opp2
opp2_cis90<-est_opp2+c(-1,1)*1.65*se_opp2


#Interacting performance with junior coalition partner
mlm2_junior_int<-glmer(cons~cons_lag+
                         vdiff*junior+
                         daystoelec+
                         poldist+
                         enp+
                         propdayscovered+
                         cohthey_0day+
                         (1|party_camp), family=binomial, pd[pd$pm==0,])
ss <- getME(mlm2_junior_int,c("theta","fixef"))
mlm2_junior_int <- update(mlm2_junior_int,start=ss,control=glmerControl(optCtrl=list(maxfun=2e9)))

#Get Estimate and SE
est_inc3<-fixef(mlm2_junior_int)[3]
se_inc3<-sqrt(vcov(mlm2_junior_int)[3,3])
inc3_cis95<-est_inc3+c(-1,1)*1.96*se_inc3
inc3_cis90<-est_inc3+c(-1,1)*1.65*se_inc3
est_opp3<-fixef(mlm2_junior_int)[3]+fixef(mlm2_junior_int)[10]
se_opp3<-sqrt(vcov(mlm2_junior_int)[3,3] + vcov(mlm2_junior_int)[10,10] + 2*vcov(mlm2_junior_int)[3,10])
opp3_cis95<-est_opp3+c(-1,1)*1.96*se_opp3
opp3_cis90<-est_opp3+c(-1,1)*1.65*se_opp3


#%#%#%#%#%#%
# # Figure F1 (panel a)
coefplot(c(est_inc1,est_opp1),c(se_inc1,se_opp1), 
         col.pts=c("aquamarine3"), cex.pts=1.4, cex.var=1, mar=c(1,5,5,1),oma=c(2,1,0,1),
         varnames=rev(c("Government","Opposition")), main="", lower.conf.bounds=c(inc1_cis95[1],opp1_cis95[1]),
         upper.conf.bounds=c(inc1_cis95[2],opp1_cis95[2]),
         
         xlim=c(-.15,.15))
lines(c(inc1_cis90[1],inc1_cis90[2]),c(1,1),col='aquamarine3',lwd=3)
lines(c(opp1_cis90[1],opp1_cis90[2]),c(2,2),col='aquamarine3',lwd=3)
mtext("Marginal effects of\ncampaign performance", outer=F, line=2.5, cex=1)

#%#%#%#%#%#%
# # Figure F1 (panel b)
coefplot(c(est_inc2,est_opp2),c(se_inc2,se_opp2), 
         col.pts=c("aquamarine3"), cex.pts=1.4, cex.var=1, mar=c(1,5,5,1),oma=c(2,1,0,1),
         varnames=rev(c("Controls PM","Opposition")), main="", lower.conf.bounds=c(inc2_cis95[1],opp2_cis95[1]),
         upper.conf.bounds=c(inc2_cis95[2],opp2_cis95[2]),
         
         xlim=c(-.15,.15))
lines(c(inc2_cis90[1],inc2_cis90[2]),c(1,1),col='aquamarine3',lwd=3)
lines(c(opp2_cis90[1],opp2_cis90[2]),c(2,2),col='aquamarine3',lwd=3)
mtext("Marginal effects of\ncampaign performance", outer=F, line=2.5, cex=1)


#%#%#%#%#%#%
# # Figure F1 (panel c)
coefplot(c(est_inc3,est_opp3),c(se_inc3,se_opp3), 
         col.pts=c("aquamarine3"), cex.pts=1.4, cex.var=1, mar=c(1,5,5,1),oma=c(2,1,0,1),
         varnames=rev(c("Junior Member","Opposition")), main="", lower.conf.bounds=c(inc3_cis95[1],opp3_cis95[1]),
         upper.conf.bounds=c(inc3_cis95[2],opp3_cis95[2]),
         
         xlim=c(-.15,.15))
lines(c(inc3_cis90[1],inc3_cis90[2]),c(1,1),col='aquamarine3',lwd=3)
lines(c(opp3_cis90[1],opp3_cis90[2]),c(2,2),col='aquamarine3',lwd=3)
mtext("Marginal effects of\ncampaign performance", outer=F, line=2.5, cex=1)



#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%
#APPENDIX G - Campaign responsiveness for Radical-Right and Green parties
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%

set.seed(423)
rrw2<-glmer(st_rrw~vdiff+daystoelec+propdayscovered+(1|party_camp), family='binomial',pd[pd$rrw==1,])
gr2<-glmer(st_green~vdiff+daystoelec+propdayscovered+(1|party_camp), family='binomial',pd[pd$green==1,])

#Combined measure
pd$rg_comb_bin<-ifelse(pd$green==1,pd$st_green,
                       ifelse(pd$rrw==1,pd$st_rrw,  NA))
comb2<-glmer(rg_comb_bin~vdiff+daystoelec+propdayscovered+(1|party_camp),family='binomial',pd)

stargazer(rrw2,gr2,comb2, digits=2,
          covariate.labels=c('Non-Centrist Rhetoric', 'Days to Election', '\\% Days Covered'),
          star.char = c('+', '*', '**'), no.space=T)

# # # PREDICTIONS
preds_comb1<-predict(comb2,data.frame(vdiff=0, 
                                      daystoelec=15,
                                      propdayscovered=mean(pd$propdayscovered),
                                      camp='UK2010',
                                      party_camp='LabourUK2010'
), type='response', allow.new.level=T)

preds_comb2<-predict(comb2,data.frame(vdiff=5, 
                                      daystoelec=15,
                                      propdayscovered=mean(pd$propdayscovered),
                                      camp='UK2010',
                                      party_camp='LabourUK2010'
), type='response', allow.new.level=T)

difprob<-preds_comb2-preds_comb1
difprob #Difference in probability of making statement on owned issue

